#pragma once
#include<assert.h>

namespace stc 
{
	template<class T>
	class vector
	{
	public:
		typedef T* iterator;
		typedef const T* const_iterator;

		vector()
		{

		}

		vector(const vector<T>& v)
		{
			reserve(v.capacity());
			for (auto& e : v)
			{
				push_back(e);
			}
		}

		vector(size_t n, const T& val = T())
		{
			reserve(n);
			for (size_t i = 0; i < n; i++)
				push_back(val);
		}

		~vector()
		{
			delete[]_start;
			_start = _finish = _endofstorage = nullptr;
		}
		
		iterator begin()
		{
			return _start;
		}

		iterator end()
		{
			return _finish;
		}

		const_iterator begin()const
		{
			return _start;
		}

		size_t size()
		{
			return _finish - _start;
		}

		size_t capacity()const
		{
			return _endofstorage - _start;
		}

		T& operator[](size_t pos)
		{
			assert(pos < size());

			return _start[pos];
		}

		const T& operator[](size_t pos)const
		{
			assert(pos < size());

			return _start[pos];
		}

		const_iterator end()const
		{
			return _finish;
		}

		void reserve(int n)
		{
			if (n > capacity())
			{
				T* tmp = new T[n];
				size_t old_size = size();
				//memcpy(tmp, _start, size() * sizeof(T));
				for (int i = 0; i < old_size; i++)
				{
					tmp[i]=_start[i];
				}
				delete[]_start;

				_start = tmp;
				_finish = tmp + old_size;
				_endofstorage = tmp + n;
			}
		}

		void push_back(const T& val)
		{
			if (_finish == _endofstorage)
			{
				reserve(capacity() == 0 ? 4 : capacity() * 2);
			}
			*_finish = val;
			++_finish;
		}

		void pop_back()
		{
			assert(!empty());

			--_finish;
		}

		void insert(iterator pos, const T& val)
		{
			assert(pos >= _start && pos <= _finish);
			if (_finish == _endofstorage)
			{
				size_t len = pos - _start;

				reserve(capacity() == 0 ? 4 : capacity() * 2);

				pos = _start + len;
			}

			iterator it = end()-1;
			while (it >= pos)
			{
				*(it + 1) = *it;
				it--;
			}

			*pos = val;
			_finish++;

		}

		void resize(size_t n, const T& val = T())
		{
			if (n > size())
			{
				reserve(n);
				while (_finish < _start + n)
				{
					*_finish = val;
					_finish++;
				}
			}

			else
			{
				_finish = _start + n;
			}
		}

		void erase(iterator pos)
		{
			assert(pos >= _start && pos <= _finish);

			iterator it = pos + 1;

			while (it < _finish)
			{
				*(it - 1) = *it;
				it++;
			}

			_finish--;
		}

		bool empty()
		{
			return _start == _finish;
		}

		void swap(vector<T>& v)
		{
			std::swap(_start, v._start); 
			std::swap(_finish, v._finish);
			std::swap(_endofstorage, v.endofstorage);
		}

		vector<T>& operator=(vector<T> v)
		{
			swap(v);
			return *this;
		}

	private:
		iterator _start = nullptr;
		iterator _finish = nullptr;
		iterator _endofstorage=nullptr;

	};


	void test()
	{
		vector<int>v;
		v.push_back(1);
		v.push_back(1);
		v.push_back(1);
		v.push_back(1);
		v.insert(v.begin(), 2);
		v.pop_back();
		v.resize(10, 2);
		vector<int>v1 = v;
		for (int i = 0; i < v1.size(); i++)
			cout << v1[i] << " ";
		cout << endl;
		vector<int>v2(10, 5);
		for (auto e : v2)
			cout << e << " ";
		cout << endl;
		vector<string>v3;
		v3.push_back("abc");
		cout << v3[0];
	}
}